/*
群主管理功能节点(在牛牛项目中叫做俱乐部),主要用来控制管理哪些玩家可以加入该群主的游戏房间
*/
cc.Class({
    extends: cc.Component,

    properties: {
        parent:{
            default:null,
            type:cc.Node
        },
        userItem:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.isInit = true;

        this.itemBeginY = -50;
        this.itemOffsetY = -110;
        this.viewHeightOri = 430;
        //xPos：-290，-100，100，290
        this.userView = this.node.getChildByName("userView");
        this.userContent = this.userView.getChildByName("view").getChildByName("content");
        
        this.editNode = this.node.getChildByName("editNode");
        this.idEditBox = this.editNode.getChildByName("idEdit").getComponent("cc.EditBox");
        this.userName = this.editNode.getChildByName("userName").getComponent("cc.Label");

        this.switchTypeJoin = false;
        this.switchJoin = this.node.getChildByName("switchJoin").getChildByName("check").getComponent("cc.Toggle");
        this.switchTypeScore = false;
        this.switchScore = this.node.getChildByName("switchScore").getChildByName("check").getComponent("cc.Toggle");

        this.curUserCount = 0;
        this.userItemList = {};
        this.headImgList = {};
        this.curClubData = {};
        this.curUserInfoList = {};
        this.curUserScoreList = {};

        this.btnAddOK = this.editNode.getChildByName("btnOK").getComponent("cc.Button");
        this.btnRemove = this.node.getChildByName("btnRemove").getComponent("cc.Button");
        this.selectId = -1;
        this.selectNick = -1;
        this.tipsNode = this.node.getChildByName("tipsNode");
        this.tipsLabel = this.tipsNode.getChildByName("tipsLabel").getComponent("cc.Label");

        this.changeScoreNode = this.node.getChildByName("changeScoreNode");
        this.changeScoreNodeCurScore = this.changeScoreNode.getChildByName("curScore").getComponent("cc.Label");
        this.changeScoreNodeUserInfo = this.changeScoreNode.getChildByName("userInfo").getComponent("cc.Label");
        this.changeScoreNodeTitle = this.changeScoreNode.getChildByName("title").getComponent("cc.Label");
        this.changeScoreNodeScoreEdit = this.changeScoreNode.getChildByName("scoreEdit").getComponent("cc.EditBox");
        this.changeScoreType = 1;//1->user;2->roomScore
        this.curChangeScoreID = 0;
        this.curRoomMinScore = 0;
        this.minScoreNode = this.node.getChildByName("minScore");
        this.minScoreLabel = this.node.getChildByName("minScore").getChildByName("minScoreLabel").getComponent("cc.Label");
    },

    btnClickWithIndex:function(event, customEventData){
        var index = parseInt(customEventData);
        switch(index){
            case 0:   //btnAdd
                this.editNode.active  = true;
                this.btnAddOK.interactable = false;
                this.idEditBox.string = "";
                this.userName.string = "用户昵称:";
                break;
            case 1:   //btnRemove
                this.tipsLabel.string = "删除成员:"+this.selectNick;
                this.tipsNode.active = true;
                break;
            case 2:   //btnOK
                var curAddId = parseInt(this.idEditBox.string);
                var self = this;
                pomelo.request("connector.club.addClubPlayer",{"playerId" : curAddId}, function(data) {
                    console.log(data);
                    if(data.flag == true){
                        self.addOneUser(data.data);
                        self.editNode.active = false;
                    }
                });

                break;
            case 3:   //btnCancle
                this.editNode.active = false;
                break;
            case 4:   //switchJoin
                this.switchTypeJoin = !this.switchTypeJoin;
                pomelo.request("connector.club.clubSwitch",{"flag" : this.switchTypeJoin}, function(data) {
                    console.log(data);
                });   
                break;
            case 5:   //tipsBox ok
                var self = this;
                pomelo.request("connector.club.removeClubPlayer",{"playerId" : parseInt(self.selectId)}, function(data) {
                    console.log(data);
                    if(data.flag == true)
                        self.removeOneUser(parseInt(self.selectId));
                });
                break;
            case 6:  //tipsBox cancle
                this.tipsNode.active = false;
                break;
            case 7:   //switchScore是否开启积分模式
                this.switchTypeScore = !this.switchTypeScore;
                if(this.switchTypeScore)
                    this.minScoreNode.active = true;
                else
                    this.minScoreNode.active = false;
                pomelo.request("connector.club.clubScoreSwitch", {flag : this.switchTypeScore}, function(data) {
                    console.log("clubScoreSwitch!!!!")
                    console.log(data)
                });
                break;
            case 8:    //setMinScore显示设置分数界面
                this.showChangeScoreNode(0);
                break;
        };
    },
    
    idEditEnd:function(){
        var self = this;
        var curUid = parseInt(this.idEditBox.string);
        pomelo.request("monitor.handle.queryNickName", {uid : curUid}, function(data) {
            console.log(data);
            if(data.flag == false)
            {
                console.log("查询失败！！！");
                self.userName.string = "查询失败请重新输入";
                self.btnAddOK.interactable = false;
                self.idEditBox.string = "";
            }else{
                console.log("查询成功！！！");
                self.userName.string = "用户昵称:" + data;
                self.btnAddOK.interactable = true;
            }
        });
    },

    reloadClubData:function(clubData){
        this.userContent.y = 0;
        this.curClubData = clubData.data;
        if(this.curClubData.switch == "true"){
            this.switchTypeJoin = true;
            this.switchJoin.isChecked = true;
        }
        if(this.curClubData.switch == "false"){
            this.switchTypeJoin = false;
            this.switchJoin.isChecked = false;
        }
        if(this.curClubData.scoreSwitch == "true"){
            this.switchTypeScore = true;
            this.switchScore.isChecked = true;
            this.minScoreNode.active = true;
        }
        if(this.curClubData.scoreSwitch == "false"){
            this.switchTypeScore = false;
            this.switchScore.isChecked = false;
            this.minScoreNode.active = false;
        }
        if(this.curClubData.minScore == null){
            this.curRoomMinScore = 0;
            this.setMinScoreLabel("未设置");
        }else{
            this.curRoomMinScore = this.curClubData.minScore;
            this.setMinScoreLabel(this.curRoomMinScore);
        }
        
        
        this.curUserScoreList = {};
        this.curUserInfoList = {};
        var self = this;
        this.curUserCount = 0;
        for(var i in this.curClubData)
        {
            if(i.indexOf(":") > 0)
            {
                var curID = i.split(":")[0];
                this.curUserScoreList[curID] = this.curClubData[i];
            }else{
                if(i != "switch" && i != "scoreSwitch" && i != "minScore")
                    this.curUserInfoList[i] = JSON.parse(this.curClubData[i]);
            }
        }
        for(var i in this.curUserInfoList)
            this.addOneItem(i,this.curUserInfoList[i]);
    },

    cleanClubData:function(){
        for(var i in this.userItemList)
            this.userItemList[i].destroy();
        this.userItemList = {};
        this.btnRemove.interactable = false;
        this.selectId = -1;
    },

    selectWithId:function(curUserId){
        this.btnRemove.interactable = true;
        this.selectId = curUserId;
    },

    addOneUser:function(data){
        this.curClubData[parseInt(data.uid)] = JSON.stringify(data);
        this.addOneItem(data.uid,data);
    },

    removeOneUser:function(index){
        this.userItemList[index].active = false;
        var curUserIndex = this.userItemList[index].userIndex;
        console.log("curUserIndex === " + curUserIndex);
        for(var i in this.userItemList)
        {
            if(this.userItemList[i].userIndex > curUserIndex)
                this.userItemList[i].userIndex --;
        }
        this.curUserCount --;
        for(var i in this.userItemList){
            this.userItemList[i].y = this.itemBeginY + this.itemOffsetY*this.userItemList[i].userIndex;
            if(this.userItemList[i].userIndex == 0)
                this.userItemList[i].getChildByName("line").active = false;
        }
        
        this.tipsNode.active = false;
        this.btnRemove.interactable = false;
        this.selectId = -1;
    },

    addOneItem:function(userId,userData){
        if(userData.nickname == null || userId == null)
            return;
        var self = this;
        // console.log("curUserId === "+userId);
        var newUserItem = cc.instantiate(this.userItem);
        newUserItem.active = true;
        this.userContent.addChild(newUserItem);
        this.userItemList[userId] = newUserItem;
        newUserItem.y = this.itemBeginY + this.itemOffsetY*this.curUserCount;
                
        newUserItem.userIndex = this.curUserCount;
        var curUserData = userData;
        newUserItem.getChildByName("headBox1").active = false;  //选中框
        var curUserHead = newUserItem.getChildByName("headImg").getComponent("cc.Sprite");
        if(this.headImgList[userId])
        {
            curUserHead.spriteFrame = this.headImgList[userId];
        }else{
            this.getWXHearFrameNoSave(curUserData.head,curUserHead,userId);
        }
        newUserItem.getChildByName("name").getComponent("cc.Label").string = curUserData.nickname;
        newUserItem.nickString = curUserData.nickname;
        newUserItem.getChildByName("id").getComponent("cc.Label").string = curUserData.uid;

        if(this.curUserScoreList[userId] != null)
            newUserItem.getChildByName("score").getComponent("cc.Label").string = this.curUserScoreList[userId];
        else
            newUserItem.getChildByName("score").getComponent("cc.Label").string = "0";
        var curUserSelect = newUserItem.getChildByName("btnSelect").getComponent("cc.Button");
        var selectCallBack = function(){
            console.log("选择用户"+selectCallBack.id);
            if(self.selectId != -1)
                self.userItemList[self.selectId].getChildByName("headBox1").active = false;

            self.selectWithId(selectCallBack.id);
            self.userItemList[selectCallBack.id].getChildByName("headBox1").active = true;
            self.selectNick = self.userItemList[selectCallBack.id].nickString;
        };
        selectCallBack.id = userId;
        if(this.curUserCount == 0)
            newUserItem.getChildByName("line").active = false;

        curUserSelect.node.on(cc.Node.EventType.TOUCH_END, selectCallBack, self);

        var btnChangeScore = newUserItem.getChildByName("btnChange").getComponent("cc.Button");
        var changeScoreCallBack = function(){
            self.showChangeScoreNode(changeScoreCallBack.id);
        };
        changeScoreCallBack.id = userId;
        btnChangeScore.node.on(cc.Node.EventType.TOUCH_END, changeScoreCallBack, self);

        this.curUserCount ++;
        this.userContent.height = this.userContent.height - this.itemOffsetY*this.curUserCount;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.node.active = true;
    },

    hideLayer:function(){
        this.node.active = false;
        this.cleanClubData();
    },

    getWXHearFrameNoSave:function(headUrl,sprite,uid) {
        if(headUrl == ""){
            console.log("fuck@@@@@@@@@")
            return;
        }
        console.log("getWXHearFrameNoSave");
        var self = this;
        cc.loader.load({url:headUrl,type:'png'}, function (err, texture) {
            var newFrame = new cc.SpriteFrame(texture);
            if(sprite)
                sprite.spriteFrame = newFrame;
            self.headImgList[uid] = newFrame;
        });
    },

    showChangeScoreNode:function(curUserId){
        console.log("curUserId === "+curUserId);
        if(curUserId)
        {
            this.changeScoreType = 1;
            this.curChangeScoreID = curUserId;
            if(this.curUserScoreList[curUserId] == null)
                this.changeScoreNodeCurScore.string = "当前分数值:0";
            else
                this.changeScoreNodeCurScore.string = "当前分数值:" + this.curUserScoreList[curUserId];
            this.changeScoreNodeTitle.string = "玩家积分设置"
            this.changeScoreNodeUserInfo.string = "昵称:"+this.curUserInfoList[curUserId].nickname+"   ID:"+this.curUserInfoList[curUserId].uid;
        }else{
            this.changeScoreType = 2;
            this.curChangeScoreID = 0;
            this.changeScoreNodeTitle.string = "准入分设置";
            this.changeScoreNodeUserInfo.string = "";
            this.changeScoreNodeCurScore.string = "当前分数值:"+this.curRoomMinScore;
        }
        this.changeScoreNode.active = true;
    },

    hideChangeScoreNode:function(){
        this.changeScoreNode.active = false;
    },

    changeScoreNodeOKClick:function(){
        var self = this
        console.log("changeScoreNodeOKClick!!!!")
        var newScore = parseInt(this.changeScoreNodeScoreEdit.string);
        if(this.changeScoreType == 1)
        {
            pomelo.request("connector.club.changeClubScore", {uid : this.curChangeScoreID,score : newScore}, function(data) {
                console.log("changeClubScore!!!!!")
                console.log(data)
                if(data.flag)
                {
                    self.changeScoreNodeScoreEdit.string = "";
                    self.userItemList[self.curChangeScoreID].getChildByName("score").string = newScore;
                }
                self.changeScoreNode.active = false;
            });
        }
        if(this.changeScoreType == 2)
        {
            pomelo.request("connector.club.setClubScore", {minScore : newScore}, function(data) {
                console.log("setClubScore!!!!!")
                console.log(data)
                if(data.flag)
                {
                    self.setMinScoreLabel(newScore);
                    self.curRoomMinScore = newScore;
                    self.changeScoreNodeScoreEdit.string = "";
                }
                self.changeScoreNode.active = false;
                
            });
        }
    },

    setMinScoreLabel:function(curScore){
        this.minScoreLabel.string = "最低准入分:\n" + curScore;
    },
});
